home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / msds-prl / bcdsprl4.zoo / perl.c < prev    next >
C/C++ Source or Header  |  1991-12-25  |  37KB  |  1,456 lines

  1. char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $\nPatch level: ###\n";
  2. /*
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    perl.c,v $
  9.  * Revision 4.0.1.6  91/11/11  16:38:45  lwall
  10.  * patch19: default arg for shift was wrong after first subroutine definition
  11.  * patch19: op/regexp.t failed from missing arg to bcmp()
  12.  *
  13.  * Revision 4.0.1.5  91/11/05  18:03:32  lwall
  14.  * patch11: random cleanup
  15.  * patch11: $0 was being truncated at times
  16.  * patch11: cppstdin now installed outside of source directory
  17.  * patch11: -P didn't allow use of #elif or #undef
  18.  * patch11: prepared for ctype implementations that don't define isascii()
  19.  * patch11: added eval {}
  20.  * patch11: eval confused by string containing null
  21.  *
  22.  * Revision 4.0.1.4  91/06/10  01:23:07  lwall
  23.  * patch10: perl -v printed incorrect copyright notice
  24.  *
  25.  * Revision 4.0.1.3  91/06/07  11:40:18  lwall
  26.  * patch4: changed old $^P to $^X
  27.  *
  28.  * Revision 4.0.1.2  91/06/07  11:26:16  lwall
  29.  * patch4: new copyright notice
  30.  * patch4: added $^P variable to control calling of perldb routines
  31.  * patch4: added $^F variable to specify maximum system fd, default 2
  32.  * patch4: debugger lost track of lines in eval
  33.  *
  34.  * Revision 4.0.1.1  91/04/11  17:49:05  lwall
  35.  * patch1: fixed undefined environ problem
  36.  *
  37.  * Revision 4.0  91/03/20  01:37:44  lwall
  38.  * 4.0 baseline.
  39.  *
  40.  */
  41.  
  42.  
  43. /*SUPPRESS 560*/
  44.  
  45.  
  46. #include "EXTERN.h"
  47. #include "perl.h"
  48. #include "perly.h"
  49. #ifdef MSDOS
  50. #include "patchlev.h"
  51. #include <process.h>
  52. #include <dos.h>
  53. #else
  54. #include "patchlevel.h"
  55. #endif
  56.  
  57.  
  58. char *getenv();
  59.  
  60.  
  61. #ifdef IAMSUID
  62. #ifndef DOSUID
  63. #define DOSUID
  64. #endif
  65. #endif
  66.  
  67.  
  68. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  69. #ifdef DOSUID
  70. #undef DOSUID
  71. #endif
  72. #endif
  73.  
  74.  
  75. static char* moreswitches();
  76. static char* cddir;
  77. static bool minus_c;
  78. static char patchlevel[6];
  79. static char *nrs = "\n";
  80. static int nrschar = '\n';      /* final char of rs, or 0777 if none */
  81. static int nrslen = 1;
  82.  
  83.  
  84. main(argc,argv,env)
  85. register int argc;
  86. register char **argv;
  87. register char **env;
  88. {
  89.     register STR *str;
  90.     register char *s;
  91.     char *scriptname;
  92.     char *getenv();
  93.     bool dosearch = FALSE;
  94. #ifdef DOSUID
  95.     char *validarg = "";
  96. #endif
  97.  
  98.  
  99. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  100. #ifdef IAMSUID
  101. #undef IAMSUID
  102.     fatal("suidperl is no longer needed since the kernel can now execute\n\
  103. setuid perl scripts securely.\n");
  104. #endif
  105. #endif
  106.  
  107.  
  108.     origargv = argv;
  109.     origargc = argc;
  110.     origenviron = environ;
  111.     uid = (int)getuid();
  112.     euid = (int)geteuid();
  113.     gid = (int)getgid();
  114.     egid = (int)getegid();
  115.     sprintf(patchlevel,"%3.3s%2.2d", index(rcsid,'4'), PATCHLEVEL);
  116. #ifdef MSDOS
  117.     /*
  118.      * There is no way we can refer to them from Perl so close them to save
  119.      * space.  The other alternative would be to provide STDAUX and STDPRN
  120.      * filehandles.
  121.      */
  122.     (void)fclose(stdaux);
  123.     (void)fclose(stdprn);
  124. #ifdef __OVERLAY__
  125.     _OvrInitEms(0,0,32);
  126. #endif
  127. #endif
  128.     if (do_undump) {
  129.     origfilename = savestr(argv[0]);
  130.     do_undump = 0;
  131.     loop_ptr = -1;        /* start label stack again */
  132.     goto just_doit;
  133.     }
  134.     (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
  135.     linestr = Str_new(65,80);
  136.     str_nset(linestr,"",0);
  137.     str = str_make("",0);        /* first used for -I flags */
  138.     curstash = defstash = hnew(0);
  139.     curstname = str_make("main",4);
  140.     stab_xhash(stabent("_main",TRUE)) = defstash;
  141.     defstash->tbl_name = "main";
  142.     incstab = hadd(aadd(stabent("INC",TRUE)));
  143.     incstab->str_pok |= SP_MULTI;
  144.     for (argc--,argv++; argc > 0; argc--,argv++) {
  145.     if (argv[0][0] != '-' || !argv[0][1])
  146.         break;
  147. #ifdef DOSUID
  148.     if (*validarg)
  149.     validarg = " PHOOEY ";
  150.     else
  151.     validarg = argv[0];
  152. #endif
  153.     s = argv[0]+1;
  154.       reswitch:
  155.     switch (*s) {
  156.     case '0':
  157.     case 'a':
  158.     case 'c':
  159.     case 'd':
  160.     case 'D':
  161.     case 'i':
  162.     case 'l':
  163.     case 'n':
  164.     case 'p':
  165.     case 'u':
  166.     case 'U':
  167.     case 'v':
  168.     case 'w':
  169.         if (s = moreswitches(s))
  170.         goto reswitch;
  171.         break;
  172.  
  173.  
  174.     case 'e':
  175. #ifdef TAINT
  176.         if (euid != uid || egid != gid)
  177.         fatal("No -e allowed in setuid scripts");
  178. #endif
  179.         if (!e_fp) {
  180.             e_tmpname = savestr(TMPPATH);
  181.         (void)mktemp(e_tmpname);
  182.         e_fp = fopen(e_tmpname,"w");
  183.         if (!e_fp)
  184.             fatal("Cannot open temporary file");
  185.         }
  186.         if (argv[1]) {
  187.         fputs(argv[1],e_fp);
  188.         argc--,argv++;
  189.         }
  190.         (void)putc('\n', e_fp);
  191.         break;
  192.     case 'I':
  193. #ifdef TAINT
  194.         if (euid != uid || egid != gid)
  195.         fatal("No -I allowed in setuid scripts");
  196. #endif
  197.         str_cat(str,"-");
  198.         str_cat(str,s);
  199.         str_cat(str," ");
  200.         if (*++s) {
  201.         (void)apush(stab_array(incstab),str_make(s,0));
  202.         }
  203.         else if (argv[1]) {
  204.         (void)apush(stab_array(incstab),str_make(argv[1],0));
  205.         str_cat(str,argv[1]);
  206.         argc--,argv++;
  207.         str_cat(str," ");
  208.         }
  209.         break;
  210.     case 'P':
  211. #ifdef TAINT
  212.         if (euid != uid || egid != gid)
  213.         fatal("No -P allowed in setuid scripts");
  214. #endif
  215.         preprocess = TRUE;
  216.         s++;
  217.         goto reswitch;
  218.     case 's':
  219. #ifdef TAINT
  220.         if (euid != uid || egid != gid)
  221.         fatal("No -s allowed in setuid scripts");
  222. #endif
  223.         doswitches = TRUE;
  224.         s++;
  225.         goto reswitch;
  226.     case 'S':
  227. #ifdef TAINT
  228.         if (euid != uid || egid != gid)
  229.         fatal("No -S allowed in setuid scripts");
  230. #endif
  231.         dosearch = TRUE;
  232.         s++;
  233.         goto reswitch;
  234.     case 'x':
  235.         doextract = TRUE;
  236.         s++;
  237.         if (*s)
  238.         cddir = savestr(s);
  239.         break;
  240.     case '-':
  241.         argc--,argv++;
  242.         goto switch_end;
  243.     case 0:
  244.         break;
  245.     default:
  246.         fatal("Unrecognized switch: -%s",s);
  247.     }
  248.     }
  249.   switch_end:
  250.     scriptname = argv[0];
  251.     if (e_fp) {
  252.     (void)fclose(e_fp);
  253.     argc++,argv--;
  254.     scriptname = e_tmpname;
  255.     }
  256.  
  257.  
  258. #ifdef MSDOS
  259. #define PERLLIB_SEP ';'
  260. #else
  261. #define PERLLIB_SEP ':'
  262. #endif
  263. #ifndef TAINT        /* Can't allow arbitrary PERLLIB in setuid script */
  264.     {
  265.     char * s2 = getenv("PERLLIB");
  266.  
  267.  
  268.     if ( s2 ) {
  269.         /* Break at all separators */
  270.         while ( *s2 ) {
  271.         /* First, skip any consecutive separators */
  272.         while ( *s2 == PERLLIB_SEP ) {
  273.             /* Uncomment the next line for PATH semantics */
  274.             /* (void)apush(stab_array(incstab),str_make(".",1)); */
  275.             s2++;
  276.         }
  277.         if ( (s = index(s2,PERLLIB_SEP)) != Nullch ) {
  278.             (void)apush(stab_array(incstab),str_make(s2,(int)(s-s2)));
  279.             s2 = s+1;
  280.         } else {
  281.             (void)apush(stab_array(incstab),str_make(s2,0));
  282.             break;
  283.         }
  284.         }
  285.     }
  286.     }
  287. #endif /* TAINT */
  288.  
  289.  
  290. #ifndef PRIVLIB
  291. #define PRIVLIB "/usr/local/lib/perl"
  292. #endif
  293.     (void)apush(stab_array(incstab),str_make(PRIVLIB,0));
  294.     (void)apush(stab_array(incstab),str_make(".",1));
  295.  
  296.  
  297.     str_set(&str_no,No);
  298.     str_set(&str_yes,Yes);
  299.  
  300.  
  301.     /* open script */
  302.  
  303.  
  304.     if (scriptname == Nullch)
  305. #ifdef MSDOS
  306.     {
  307.     if ( isatty(fileno(stdin)) )
  308.       moreswitches("v");
  309.     scriptname = "-";
  310.     }
  311. #else
  312.     scriptname = "-";
  313. #endif
  314.     if (dosearch && !index(scriptname, '/') && (s = getenv("PATH"))) {
  315.     char *xfound = Nullch, *xfailed = Nullch;
  316.     int len;
  317.  
  318.  
  319.     bufend = s + strlen(s);
  320.     while (*s) {
  321. #ifndef MSDOS
  322.         s = cpytill(tokenbuf,s,bufend,':',&len);
  323. #else
  324.         for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
  325.         tokenbuf[len] = '\0';
  326. #endif
  327.         if (*s)
  328.         s++;
  329. #ifndef MSDOS
  330.         if (len && tokenbuf[len-1] != '/')
  331. #else
  332.         if (len && tokenbuf[len-1] != '\\')
  333. #endif
  334.         (void)strcat(tokenbuf+len,"/");
  335.         (void)strcat(tokenbuf+len,scriptname);
  336. #ifdef DEBUGGING
  337.         if (debug & 1)
  338.         fprintf(stderr,"Looking for %s\n",tokenbuf);
  339. #endif
  340.         if (stat(tokenbuf,&statbuf) < 0)        /* not there? */
  341.         continue;
  342.         if (S_ISREG(statbuf.st_mode)
  343.          && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
  344.         xfound = tokenbuf;              /* bingo! */
  345.         break;
  346.         }
  347.         if (!xfailed)
  348.         xfailed = savestr(tokenbuf);
  349.     }
  350.     if (!xfound)
  351.         fatal("Can't execute %s", xfailed ? xfailed : scriptname );
  352.     if (xfailed)
  353.         Safefree(xfailed);
  354.     scriptname = savestr(xfound);
  355.     }
  356.  
  357.  
  358.     fdpid = anew(Nullstab);    /* for remembering popen pids by fd */
  359.     pidstatus = hnew(COEFFSIZE);/* for remembering status of dead pids */
  360.  
  361.  
  362.     origfilename = savestr(scriptname);
  363.     curcmd->c_filestab = fstab(origfilename);
  364.     if (strEQ(origfilename,"-"))
  365.     scriptname = "";
  366.     if (preprocess) {
  367.     char *cpp = CPPSTDIN;
  368.  
  369.  
  370.     if (strEQ(cpp,"cppstdin"))
  371.         sprintf(tokenbuf, "%s/%s", SCRIPTDIR, cpp);
  372.     else
  373.         sprintf(tokenbuf, "%s", cpp);
  374.     str_cat(str,"-I");
  375.     str_cat(str,PRIVLIB);
  376.     (void)sprintf(buf, "\
  377. %ssed %s -e '/^[^#]/b' \
  378.  -e '/^#[     ]*include[     ]/b' \
  379.  -e '/^#[     ]*define[     ]/b' \
  380.  -e '/^#[     ]*if[     ]/b' \
  381.  -e '/^#[     ]*ifdef[     ]/b' \
  382.  -e '/^#[     ]*ifndef[     ]/b' \
  383.  -e '/^#[     ]*else/b' \
  384.  -e '/^#[     ]*elif[     ]/b' \
  385.  -e '/^#[     ]*undef[     ]/b' \
  386.  -e '/^#[     ]*endif/b' \
  387.  -e 's/^[     ]*#.*//' \
  388.  %s | %s -C %s %s",
  389. #ifdef MSDOS
  390.       "",
  391. #else
  392.       "/bin/",
  393. #endif
  394.       (doextract ? "-e '1,/^#/d\n'" : ""),
  395.       scriptname, tokenbuf, str_get(str), CPPMINUS);
  396. #ifdef DEBUGGING
  397.     if (debug & 64) {
  398.         fputs(buf,stderr);
  399.         fputs("\n",stderr);
  400.     }
  401. #endif
  402.     doextract = FALSE;
  403. #ifdef IAMSUID                /* actually, this is caught earlier */
  404.     if (euid != uid && !euid)    /* if running suidperl */
  405. #ifdef HAS_SETEUID
  406.         (void)seteuid(uid);        /* musn't stay setuid root */
  407. #else
  408. #ifdef HAS_SETREUID
  409.         (void)setreuid(-1, uid);
  410. #else
  411.         setuid(uid);
  412. #endif
  413. #endif
  414. #endif /* IAMSUID */
  415.     rsfp = mypopen(buf,"r");
  416.     }
  417.     else if (!*scriptname) {
  418. #ifdef TAINT
  419.     if (euid != uid || egid != gid)
  420.         fatal("Can't take set-id script from stdin");
  421. #endif
  422.     rsfp = stdin;
  423.     }
  424.     else
  425.     rsfp = fopen(scriptname,"r");
  426.     if ((FILE*)rsfp == Nullfp) {
  427. #ifdef DOSUID
  428. #ifndef IAMSUID        /* in case script is not readable before setuid */
  429.     if (euid && stat(stab_val(curcmd->c_filestab)->str_ptr,&statbuf) >= 0 &&
  430.       statbuf.st_mode & (S_ISUID|S_ISGID)) {
  431.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  432.         execv(buf, origargv);    /* try again */
  433.         fatal("Can't do setuid\n");
  434.     }
  435. #endif
  436. #endif
  437.     fatal("Can't open perl script \"%s\": %s\n",
  438.       stab_val(curcmd->c_filestab)->str_ptr, strerror(errno));
  439.     }
  440.     str_free(str);        /* free -I directories */
  441.     str = Nullstr;
  442.  
  443.  
  444.     /* do we need to emulate setuid on scripts? */
  445.  
  446.  
  447.     /* This code is for those BSD systems that have setuid #! scripts disabled
  448.      * in the kernel because of a security problem.  Merely defining DOSUID
  449.      * in perl will not fix that problem, but if you have disabled setuid
  450.      * scripts in the kernel, this will attempt to emulate setuid and setgid
  451.      * on scripts that have those now-otherwise-useless bits set.  The setuid
  452.      * root version must be called suidperl or sperlN.NNN.  If regular perl
  453.      * discovers that it has opened a setuid script, it calls suidperl with
  454.      * the same argv that it had.  If suidperl finds that the script it has
  455.      * just opened is NOT setuid root, it sets the effective uid back to the
  456.      * uid.  We don't just make perl setuid root because that loses the
  457.      * effective uid we had before invoking perl, if it was different from the
  458.      * uid.
  459.      *
  460.      * DOSUID must be defined in both perl and suidperl, and IAMSUID must
  461.      * be defined in suidperl only.  suidperl must be setuid root.  The
  462.      * Configure script will set this up for you if you want it.
  463.      *
  464.      * There is also the possibility of have a script which is running
  465.      * set-id due to a C wrapper.  We want to do the TAINT checks
  466.      * on these set-id scripts, but don't want to have the overhead of
  467.      * them in normal perl, and can't use suidperl because it will lose
  468.      * the effective uid info, so we have an additional non-setuid root
  469.      * version called taintperl or tperlN.NNN that just does the TAINT checks.
  470.      */
  471.  
  472.  
  473. #ifdef DOSUID
  474.     if (fstat(fileno(rsfp),&statbuf) < 0)    /* normal stat is insecure */
  475.     fatal("Can't stat script \"%s\"",origfilename);
  476.     if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
  477.     int len;
  478.  
  479.  
  480. #ifdef IAMSUID
  481. #ifndef HAS_SETREUID
  482.     /* On this access check to make sure the directories are readable,
  483.      * there is actually a small window that the user could use to make
  484.      * filename point to an accessible directory.  So there is a faint
  485.      * chance that someone could execute a setuid script down in a
  486.      * non-accessible directory.  I don't know what to do about that.
  487.      * But I don't think it's too important.  The manual lies when
  488.      * it says access() is useful in setuid programs.
  489.      */
  490.     if (access(stab_val(curcmd->c_filestab)->str_ptr,1))    /*double check*/
  491.         fatal("Permission denied");
  492. #else
  493.     /* If we can swap euid and uid, then we can determine access rights
  494.      * with a simple stat of the file, and then compare device and
  495.      * inode to make sure we did stat() on the same file we opened.
  496.      * Then we just have to make sure he or she can execute it.
  497.      */
  498.     {
  499.         struct stat tmpstatbuf;
  500.  
  501.  
  502.         if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
  503.         fatal("Can't swap uid and euid");    /* really paranoid */
  504.         if (stat(stab_val(curcmd->c_filestab)->str_ptr,&tmpstatbuf) < 0)
  505.         fatal("Permission denied");    /* testing full pathname here */
  506.         if (tmpstatbuf.st_dev != statbuf.st_dev ||
  507.         tmpstatbuf.st_ino != statbuf.st_ino) {
  508.         (void)fclose(rsfp);
  509.         if (rsfp = mypopen("/bin/mail root","w")) {    /* heh, heh */
  510.             fprintf(rsfp,
  511. "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
  512. (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
  513.             uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
  514.             statbuf.st_dev, statbuf.st_ino,
  515.             stab_val(curcmd->c_filestab)->str_ptr,
  516.             statbuf.st_uid, statbuf.st_gid);
  517.             (void)mypclose(rsfp);
  518.         }
  519.         fatal("Permission denied\n");
  520.         }
  521.         if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
  522.         fatal("Can't reswap uid and euid");
  523.         if (!cando(S_IXUSR,FALSE,&statbuf))        /* can real uid exec? */
  524.         fatal("Permission denied\n");
  525.     }
  526. #endif /* HAS_SETREUID */
  527. #endif /* IAMSUID */
  528.  
  529.  
  530.     if (!S_ISREG(statbuf.st_mode))
  531.         fatal("Permission denied");
  532.     if (statbuf.st_mode & S_IWOTH)
  533.         fatal("Setuid/gid script is writable by world");
  534.     doswitches = FALSE;        /* -s is insecure in suid */
  535.     curcmd->c_line++;
  536.     if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
  537.       strnNE(tokenbuf,"#!",2) )    /* required even on Sys V */
  538.         fatal("No #! line");
  539.     s = tokenbuf+2;
  540.     if (*s == ' ') s++;
  541.     while (!isSPACE(*s)) s++;
  542.     if (strnNE(s-4,"perl",4) && strnNE(s-9,"perl",4))  /* sanity check */
  543.         fatal("Not a perl script");
  544.     while (*s == ' ' || *s == '\t') s++;
  545.     /*
  546.      * #! arg must be what we saw above.  They can invoke it by
  547.      * mentioning suidperl explicitly, but they may not add any strange
  548.      * arguments beyond what #! says if they do invoke suidperl that way.
  549.      */
  550.     len = strlen(validarg);
  551.     if (strEQ(validarg," PHOOEY ") ||
  552.         strnNE(s,validarg,len) || !isSPACE(s[len]))
  553.         fatal("Args must match #! line");
  554.  
  555.  
  556. #ifndef IAMSUID
  557.     if (euid != uid && (statbuf.st_mode & S_ISUID) &&
  558.         euid == statbuf.st_uid)
  559.         if (!do_undump)
  560.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  561. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  562. #endif /* IAMSUID */
  563.  
  564.  
  565.     if (euid) {    /* oops, we're not the setuid root perl */
  566.         (void)fclose(rsfp);
  567. #ifndef IAMSUID
  568.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  569.         execv(buf, origargv);    /* try again */
  570. #endif
  571.         fatal("Can't do setuid\n");
  572.     }
  573.  
  574.  
  575.     if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid)
  576. #ifdef HAS_SETEGID
  577.         (void)setegid(statbuf.st_gid);
  578. #else
  579. #ifdef HAS_SETREGID
  580.         (void)setregid((GIDTYPE)-1,statbuf.st_gid);
  581. #else
  582.         setgid(statbuf.st_gid);
  583. #endif
  584. #endif
  585.     if (statbuf.st_mode & S_ISUID) {
  586.         if (statbuf.st_uid != euid)
  587. #ifdef HAS_SETEUID
  588.         (void)seteuid(statbuf.st_uid);    /* all that for this */
  589. #else
  590. #ifdef HAS_SETREUID
  591.         (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
  592. #else
  593.         setuid(statbuf.st_uid);
  594. #endif
  595. #endif
  596.     }
  597.     else if (uid)            /* oops, mustn't run as root */
  598. #ifdef HAS_SETEUID
  599.         (void)seteuid((UIDTYPE)uid);
  600. #else
  601. #ifdef HAS_SETREUID
  602.         (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
  603. #else
  604.         setuid((UIDTYPE)uid);
  605. #endif
  606. #endif
  607.     uid = (int)getuid();
  608.     euid = (int)geteuid();
  609.     gid = (int)getgid();
  610.     egid = (int)getegid();
  611.     if (!cando(S_IXUSR,TRUE,&statbuf))
  612.         fatal("Permission denied\n");    /* they can't do this */
  613.     }
  614. #ifdef IAMSUID
  615.     else if (preprocess)
  616.     fatal("-P not allowed for setuid/setgid script\n");
  617.     else
  618.     fatal("Script is not setuid/setgid in suidperl\n");
  619. #else
  620. #ifndef TAINT        /* we aren't taintperl or suidperl */
  621.     /* script has a wrapper--can't run suidperl or we lose euid */
  622.     else if (euid != uid || egid != gid) {
  623.     (void)fclose(rsfp);
  624.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  625.     execv(buf, origargv);    /* try again */
  626.     fatal("Can't run setuid script with taint checks");
  627.     }
  628. #endif /* TAINT */
  629. #endif /* IAMSUID */
  630. #else /* !DOSUID */
  631. #ifndef TAINT        /* we aren't taintperl or suidperl */
  632.     if (euid != uid || egid != gid) {    /* (suidperl doesn't exist, in fact) */
  633. #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
  634.     fstat(fileno(rsfp),&statbuf);    /* may be either wrapped or real suid */
  635.     if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
  636.         ||
  637.         (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
  638.        )
  639.         if (!do_undump)
  640.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  641. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  642. #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
  643.     /* not set-id, must be wrapped */
  644.     (void)fclose(rsfp);
  645.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  646.     execv(buf, origargv);    /* try again */
  647.     fatal("Can't run setuid script with taint checks");
  648.     }
  649. #endif /* TAINT */
  650. #endif /* DOSUID */
  651.  
  652.  
  653. #if !defined(IAMSUID) && !defined(TAINT)
  654.  
  655.  
  656.     /* skip forward in input to the real script? */
  657.  
  658.  
  659.     while (doextract) {
  660.     if ((s = str_gets(linestr, rsfp, 0)) == Nullch)
  661.         fatal("No Perl script found in input\n");
  662.     if (*s == '#' && s[1] == '!' && instr(s,"perl")) {
  663.         ungetc('\n',rsfp);        /* to keep line count right */
  664.         doextract = FALSE;
  665.         if (s = instr(s,"perl -")) {
  666.         s += 6;
  667.         /*SUPPRESS 530*/
  668.         while (s = moreswitches(s)) ;
  669.         }
  670.         if (cddir && chdir(cddir) < 0)
  671.         fatal("Can't chdir to %s",cddir);
  672.     }
  673.     }
  674. #endif /* !defined(IAMSUID) && !defined(TAINT) */
  675.  
  676.  
  677.     defstab = stabent("_",TRUE);
  678.  
  679.  
  680.     subname = str_make("main",4);
  681.     if (perldb) {
  682.     debstash = hnew(0);
  683.     stab_xhash(stabent("_DB",TRUE)) = debstash;
  684.     curstash = debstash;
  685.     dbargs = stab_xarray(aadd((tmpstab = stabent("args",TRUE))));
  686.     tmpstab->str_pok |= SP_MULTI;
  687.     dbargs->ary_flags = 0;
  688.     DBstab = stabent("DB",TRUE);
  689.     DBstab->str_pok |= SP_MULTI;
  690.     DBline = stabent("dbline",TRUE);
  691.     DBline->str_pok |= SP_MULTI;
  692.     DBsub = hadd(tmpstab = stabent("sub",TRUE));
  693.     tmpstab->str_pok |= SP_MULTI;
  694.     DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
  695.     tmpstab->str_pok |= SP_MULTI;
  696.     DBtrace = stab_val((tmpstab = stabent("trace",TRUE)));
  697.     tmpstab->str_pok |= SP_MULTI;
  698.     DBsignal = stab_val((tmpstab = stabent("signal",TRUE)));
  699.     tmpstab->str_pok |= SP_MULTI;
  700.     curstash = defstash;
  701.     }
  702.  
  703.  
  704.     /* init tokener */
  705.  
  706.  
  707.     bufend = bufptr = str_get(linestr);
  708.  
  709.  
  710.     savestack = anew(Nullstab);        /* for saving non-local values */
  711.     stack = anew(Nullstab);        /* for saving non-local values */
  712.     stack->ary_flags = 0;        /* not a real array */
  713.     afill(stack,63); afill(stack,-1);    /* preextend stack */
  714.     afill(savestack,63); afill(savestack,-1);
  715.  
  716.  
  717.     /* now parse the script */
  718.  
  719.  
  720.     error_count = 0;
  721.     if (yyparse() || error_count) {
  722.     if (minus_c)
  723.         fatal("%s had compilation errors.\n", origfilename);
  724.     else {
  725.         fatal("Execution of %s aborted due to compilation errors.\n",
  726.         origfilename);
  727.     }
  728.     }
  729.  
  730.  
  731.     New(50,loop_stack,128,struct loop);
  732. #ifdef DEBUGGING
  733.     if (debug) {
  734.     New(51,debname,128,char);
  735.     New(52,debdelim,128,char);
  736.     }
  737. #endif
  738.     curstash = defstash;
  739.  
  740.  
  741.     preprocess = FALSE;
  742.     if (e_fp) {
  743.     e_fp = Nullfp;
  744.     (void)UNLINK(e_tmpname);
  745.     }
  746.  
  747.  
  748.     /* initialize everything that won't change if we undump */
  749.  
  750.  
  751.     if (sigstab = stabent("SIG",allstabs)) {
  752.     sigstab->str_pok |= SP_MULTI;
  753.     (void)hadd(sigstab);
  754.     }
  755.  
  756.  
  757.     magicalize("!#?^~=-%.+&*()<>,\\/[|`':\004\t\020\024\027\006");
  758.     userinit();        /* in case linked C routines want magical variables */
  759.  
  760.  
  761.     amperstab = stabent("&",allstabs);
  762.     leftstab = stabent("`",allstabs);
  763.     rightstab = stabent("'",allstabs);
  764.     sawampersand = (amperstab || leftstab || rightstab);
  765.     if (tmpstab = stabent(":",allstabs))
  766.     str_set(STAB_STR(tmpstab),chopset);
  767.     if (tmpstab = stabent("\024",allstabs))
  768.     time(&basetime);
  769.  
  770.  
  771.     /* these aren't necessarily magical */
  772.     if (tmpstab = stabent(";",allstabs))
  773.     str_set(STAB_STR(tmpstab),"\034");
  774.     if (tmpstab = stabent("]",allstabs)) {
  775.     str = STAB_STR(tmpstab);
  776.     str_set(str,rcsid);
  777.     str->str_u.str_nval = atof(patchlevel);
  778.     str->str_nok = 1;
  779.     }
  780.     str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
  781.  
  782.  
  783.     stdinstab = stabent("STDIN",TRUE);
  784.     stdinstab->str_pok |= SP_MULTI;
  785.     stab_io(stdinstab) = stio_new();
  786.     stab_io(stdinstab)->ifp = stdin;
  787.     tmpstab = stabent("stdin",TRUE);
  788.     stab_io(tmpstab) = stab_io(stdinstab);
  789.     tmpstab->str_pok |= SP_MULTI;
  790.  
  791.  
  792.     tmpstab = stabent("STDOUT a wOS
  793. tmpstab->str_pok |= SP_MULTI;
  794.     stab_io(tmpstab) = stio_new();
  795.     stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
  796.     defoutstab = tmpstab;
  797.     tmpstab = stabent("stdout a wO    stab_io(tmpstab) = stab_io(defoutstab);
  798.     tmpstab->str_pok |= SP_MULTI;
  799.  
  800.  
  801.     curoutstab = stabent("STDERR a wO    curoutstab->str_pok |= SP_MULTI;
  802.     stab_io(curoutstab) = stio_new();
  803.     stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
  804.     tmpstab = stabent("stderr",TRUE);
  805.     stab_io(tmpstab) = stab_io(curoutstab);
  806.     tmpstab->str_pok |= SP_MULTI;
  807.     curoutstab = defoutstab;        /* switch back to STDOUT */
  808.  
  809.  
  810.     statname = Str_new(66,0);        /* last filename we did stat on */
  811.  
  812.  
  813.     /* now that script is parsed, we can modify record separator */
  814.  
  815.  
  816.     rs = nrs;
  817.     rslen = nrslen;
  818.     rschar = nrschar;
  819.     str_nset(stab_val(stabent("/", TRUE)), rs, rslen);
  820.  
  821.  
  822.     if (do_undump)
  823.     my_unexec();
  824.  
  825.  
  826.   just_doit:        /* come here if running an undumped a.out */
  827.     argc--,argv++;    /* skip name of script */
  828.     if (doswitches) {
  829.     for (; argc > 0 && **argv == '-'; argc--,argv++) {
  830.         if (argv[0][1] == '-') {
  831.         argc--,argv++;
  832.         break;
  833.         }
  834.         if (s = index(argv[0], '=')) {
  835.         *s++ = '\0';
  836.         str_set(stab_val(stabent(argv[0]+1,TRUE)),s);
  837.         }
  838.         else
  839.         str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
  840.     }
  841.     }
  842. #ifdef TAINTOS
  843. tainted = 1;
  844. #endif
  845.     if (tmpstab = stabent("0",allstabs)) {
  846.     str_set(stab_val(tmpstab),origfilename);
  847.     magicname("0", Nullch, 0);
  848.     }
  849.     if (tmpstab = stabent("\030",allstabs))
  850.     str_set(stab_val(tmpstab),origargv[0]);
  851.     if (argvstab = stabent("ARGV",allstabs)) {
  852.     argvstab->str_pok |= SP_MULTI;
  853.     (void)aadd(argvstab);
  854.     aclear(stab_array(argvstab));
  855.     for (; argc > 0; argc--,argv++) {
  856.         (void)apush(stab_array(argvstab),str_make(argv[0],0));
  857.     }
  858.     }
  859. #ifdef TAINT
  860.     (void) stabent("ENV",TRUE);        /* must test PATH and IFS */
  861. #endif
  862.     if (envstab = stabent("ENV",allstabs)) {
  863.     envstab->str_pok |= SP_MULTI;
  864.     (void)hadd(envstab);
  865.     hclear(stab_hash(envstab), FALSE);
  866.     if (env != environ)
  867.         environ[0] = Nullch;
  868.     for (; *env; env++) {
  869.         if (!(s = index(*env,'=')))
  870.         continue;
  871.         *s++ = '\0';
  872.         str = str_make(s--,0);
  873.         str_magic(str, envstab, 'E', *env, s - *env);
  874.         (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
  875.         *s = '=';
  876.     }
  877.     }
  878. #ifdef TAINT
  879.     tainted = 0;
  880. #endif
  881.     if (tmpstab = stabent("$",allstabs))
  882.     str_numset(STAB_STR(tmpstab),(double)getpid());
  883.  
  884.  
  885.     if (dowarn) {
  886.     stab_check('A','Z');
  887.     stab_check('a','z');
  888.     }
  889.  
  890.  
  891.     if (setjmp(top_env))    /* sets goto_targ on longjump */
  892.     loop_ptr = -1;        /* start label stack again */
  893.  
  894.  
  895. #ifdef DEBUGGING
  896.     if (debug & 1024)
  897.     dump_all();
  898.     if (debug)
  899.     fprintf(stderr,"\nEXECUTING...\n\n");
  900. #endif
  901.  
  902.  
  903.     if (minus_c) {
  904.     fprintf(stderr,"%s syntax OK\n", origfilename);
  905.     exit(0);
  906.     }
  907.  
  908.  
  909.     /* do it */
  910.  
  911.  
  912.     (void) cmd_exec(main_root,G_SCALAR,-1);
  913.  
  914.  
  915.     if (goto_targ)
  916.     fatal("Can't find label \"%s\"--aborting",goto_targ);
  917.     exit(0);
  918.     /* NOTREACHED */
  919. }
  920.  
  921.  
  922. void
  923. magicalize(list)
  924. register char *list;
  925. {
  926.     char sym[2];
  927.  
  928.  
  929.     sym[1] = '\0';
  930.     while (*sym = *list++)
  931.     magicname(sym, Nullch, 0);
  932. }
  933.  
  934.  
  935. void
  936. magicname(sym,name,namlen)
  937. char *sym;
  938. char *name;
  939. int namlen;
  940. {
  941.     register STAB *stab;
  942.  
  943.  
  944.     if (stab = stabent(sym,allstabs)) {
  945.     stab_flags(stab) = SF_VMAGIC;
  946.     str_magic(stab_val(stab), stab, 0, name, namlen);
  947.     }
  948. }
  949.  
  950.  
  951. void
  952. savelines(array, str)
  953. ARRAY *array;
  954. STR *str;
  955. {
  956.     register char *s = str->str_ptr;
  957.     register char *send = str->str_ptr + str->str_cur;
  958.     register char *t;
  959.     register int line = 1;
  960.  
  961.  
  962.     while (s && s < send) {
  963.     STR *tmpstr = Str_new(85,0);
  964.  
  965.  
  966.     t = index(s, '\n');
  967.     if (t)
  968.         t++;
  969.     else
  970.         t = send;
  971.  
  972.  
  973.     str_nset(tmpstr, s, t - s);
  974.     astore(array, line++, tmpstr);
  975.     s = t;
  976.     }
  977. }
  978.  
  979.  
  980. /* this routine is in perl.c by virtue of being sort of an alternate main() */
  981.  
  982.  
  983. int
  984. do_eval(str,optype,stash,savecmd,gimme,arglast)
  985. STR *str;
  986. int optype;
  987. HASH *stash;
  988. int savecmd;
  989. int gimme;
  990. int *arglast;
  991. {
  992.     STR **st = stack->ary_array;
  993.     int retval;
  994.     CMD *myroot = Nullcmd;
  995.     ARRAY *ar;
  996.     int i;
  997.     CMD * VOLATILE oldcurcmd = curcmd;
  998.     VOLATILE int oldtmps_base = tmps_base;
  999.     VOLATILE int oldsave = savestack->ary_fill;
  1000.     VOLATILE int oldperldb = perldb;
  1001.     SPAT * VOLATILE oldspat = curspat;
  1002.     SPAT * VOLATILE oldlspat = lastspat;
  1003.     static char *last_eval = Nullch;
  1004.     static long last_elen = 0;
  1005.     static CMD *last_root = Nullcmd;
  1006.     VOLATILE int sp = arglast[0];
  1007.     char *specfilename;
  1008.     char *tmpfilename;
  1009.     int parsing = 1;
  1010.  
  1011.  
  1012.     tmps_base = tmps_max;
  1013.     if (curstash != stash) {
  1014.     (void)savehptr(&curstash);
  1015.     curstash = stash;
  1016.     }
  1017.     str_set(stab_val(stabent("@",TRUE)),"");
  1018.     if (curcmd->c_line == 0)        /* don't debug debugger... */
  1019.     perldb = FALSE;
  1020.     curcmd = &compiling;
  1021.     if (optype == O_EVAL) {        /* normal eval */
  1022.     curcmd->c_filestab = fstab("(eval)");
  1023.     curcmd->c_line = 1;
  1024.     str_sset(linestr,str);
  1025.     str_cat(linestr,";\n");        /* be kind to them */
  1026.     if (perldb)
  1027.         savelines(stab_xarray(curcmd->c_filestab), linestr);
  1028.     }
  1029.     else {
  1030.     if (last_root && !in_eval) {
  1031.         Safefree(last_eval);
  1032.         last_eval = Nullch;
  1033.         cmd_free(last_root);
  1034.         last_root = Nullcmd;
  1035.     }
  1036.     specfilename = str_get(str);
  1037.     str_set(linestr,"");
  1038.     if (optype == O_REQUIRE && &str_undef !=
  1039.       hfetch(stab_hash(incstab), specfilename, strlen(specfilename), 0)) {
  1040.         curcmd = oldcurcmd;
  1041.         tmps_base = oldtmps_base;
  1042.         st[++sp] = &str_yes;
  1043.         perldb = oldperldb;
  1044.         return sp;
  1045.     }
  1046.     tmpfilename = savestr(specfilename);
  1047.     if (index("/.", *tmpfilename))
  1048.         rsfp = fopen(tmpfilename,"r");
  1049.     else {
  1050.         ar = stab_array(incstab);
  1051.         for (i = 0; i <= ar->ary_fill; i++) {
  1052.         (void)sprintf(buf, "%s/%s",
  1053.           str_get(afetch(ar,i,TRUE)), specfilename);
  1054.         rsfp = fopen(buf,"r");
  1055.         if (rsfp) {
  1056.             char *s = buf;
  1057.  
  1058.  
  1059.             if (*s == '.' && s[1] == '/')
  1060.             s += 2;
  1061.             Safefree(tmpfilename);
  1062.             tmpfilename = savestr(s);
  1063.             break;
  1064.         }
  1065.         }
  1066.     }
  1067.     curcmd->c_filestab = fstab(tmpfilename);
  1068.     Safefree(tmpfilename);
  1069.     tmpfilename = Nullch;
  1070.     if (!rsfp) {
  1071.         curcmd = oldcurcmd;
  1072.         tmps_base = oldtmps_base;
  1073.         if (optype == O_REQUIRE) {
  1074.         sprintf(tokenbuf,"Can't locate %s in @INC", specfilename);
  1075.         if (instr(tokenbuf,".h "))
  1076.             strcat(tokenbuf," (change .h to .ph maybe?)");
  1077.         if (instr(tokenbuf,".ph "))
  1078.             strcat(tokenbuf," (did you run h2ph?)");
  1079.         fatal("%s",tokenbuf);
  1080.         }
  1081.         if (gimme != G_ARRAY)
  1082.         st[++sp] = &str_undef;
  1083.         perldb = oldperldb;
  1084.         return sp;
  1085.     }
  1086.     curcmd->c_line = 0;
  1087.     }
  1088.     in_eval++;
  1089.     oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
  1090.     bufend = bufptr + linestr->str_cur;
  1091.     if (++loop_ptr >= loop_max) {
  1092.     loop_max += 128;
  1093.     Renew(loop_stack, loop_max, struct loop);
  1094.     }
  1095.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1096.     loop_stack[loop_ptr].loop_sp = sp;
  1097. #ifdef DEBUGGING
  1098.     if (debug & 4) {
  1099.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1100.     }
  1101. #endif
  1102.     eval_root = Nullcmd;
  1103.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1104.     retval = 1;
  1105.     }
  1106.     else {
  1107.     error_count = 0;
  1108.     if (rsfp) {
  1109.         retval = yyparse();
  1110.         retval |= error_count;
  1111.     }
  1112.     else if (last_root && last_elen == bufend - bufptr
  1113.       && *bufptr == *last_eval && !bcmp(bufptr,last_eval,last_elen)){
  1114.         retval = 0;
  1115.         eval_root = last_root;    /* no point in reparsing */
  1116.     }
  1117.     else if (in_eval == 1 && !savecmd) {
  1118.         if (last_root) {
  1119.         Safefree(last_eval);
  1120.         last_eval = Nullch;
  1121.         cmd_free(last_root);
  1122.         }
  1123.         last_root = Nullcmd;
  1124.         last_elen = bufend - bufptr;
  1125.         last_eval = nsavestr(bufptr, last_elen);
  1126.         retval = yyparse();
  1127.         retval |= error_count;
  1128.         if (!retval)
  1129.         last_root = eval_root;
  1130.         if (!last_root) {
  1131.         Safefree(last_eval);
  1132.         last_eval = Nullch;
  1133.         }
  1134.     }
  1135.     else
  1136.         retval = yyparse();
  1137.     }
  1138.     myroot = eval_root;        /* in case cmd_exec does another eval! */
  1139.  
  1140.  
  1141.     if (retval) {
  1142.     st = stack->ary_array;
  1143.     sp = arglast[0];
  1144.     if (gimme != G_ARRAY)
  1145.         st[++sp] = &str_undef;
  1146.     if (parsing) {
  1147. #ifndef MANGLEDPARSE
  1148. #ifdef DEBUGGING
  1149.         if (debug & 128)
  1150.         fprintf(stderr,"Freeing eval_root %lx\n",(long)eval_root);
  1151. #endif
  1152.         cmd_free(eval_root);
  1153. #endif
  1154.         if ((CMD*)eval_root == last_root)
  1155.         last_root = Nullcmd;
  1156.         eval_root = myroot = Nullcmd;
  1157.     }
  1158.     if (rsfp) {
  1159.         fclose(rsfp);
  1160.         rsfp = 0;
  1161.     }
  1162.     }
  1163.     else {
  1164.     parsing = 0;
  1165.     sp = cmd_exec(eval_root,gimme,sp);
  1166.     st = stack->ary_array;
  1167.     for (i = arglast[0] + 1; i <= sp; i++)
  1168.         st[i] = str_mortal(st[i]);
  1169.                 /* if we don't save result, free zaps it */
  1170.     if (savecmd)
  1171.         eval_root = myroot;
  1172.     else if (in_eval != 1 && myroot != last_root)
  1173.         cmd_free(myroot);
  1174.     }
  1175.  
  1176.  
  1177.     perldb = oldperldb;
  1178.     in_eval--;
  1179. #ifdef DEBUGGING
  1180.     if (debug & 4) {
  1181.     char *tmps = loop_stack[loop_ptr].loop_label;
  1182.     deb("(Popping label #%d %s)\n",loop_ptr,
  1183.         tmps ? tmps : "" );
  1184.     }
  1185. #endif
  1186.     loop_ptr--;
  1187.     tmps_base = oldtmps_base;
  1188.     curspat = oldspat;
  1189.     lastspat = oldlspat;
  1190.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1191.     restorelist(oldsave);
  1192.  
  1193.  
  1194.     if (optype != O_EVAL) {
  1195.     if (retval) {
  1196.         if (optype == O_REQUIRE)
  1197.         fatal("%s", str_get(stab_val(stabent("@",TRUE))));
  1198.     }
  1199.     else {
  1200.         curcmd = oldcurcmd;
  1201.         if (gimme == G_SCALAR ? str_true(st[sp]) : sp > arglast[0]) {
  1202.         (void)hstore(stab_hash(incstab), specfilename,
  1203.           strlen(specfilename), str_smake(stab_val(curcmd->c_filestab)),
  1204.               0 );
  1205.         }
  1206.         else if (optype == O_REQUIRE)
  1207.         fatal("%s did not return a true value", specfilename);
  1208.     }
  1209.     }
  1210.     curcmd = oldcurcmd;
  1211.     return sp;
  1212. }
  1213.  
  1214.  
  1215. int
  1216. do_try(cmd,gimme,arglast)
  1217. CMD *cmd;
  1218. int gimme;
  1219. int *arglast;
  1220. {
  1221.     STR **st = stack->ary_array;
  1222.  
  1223.  
  1224.     CMD * VOLATILE oldcurcmd = curcmd;
  1225.     VOLATILE int oldtmps_base = tmps_base;
  1226.     VOLATILE int oldsave = savestack->ary_fill;
  1227.     SPAT * VOLATILE oldspat = curspat;
  1228.     SPAT * VOLATILE oldlspat = lastspat;
  1229.     VOLATILE int sp = arglast[0];
  1230.  
  1231.  
  1232.     tmps_base = tmps_max;
  1233.     str_set(stab_val(stabent("@",TRUE)),"");
  1234.     in_eval++;
  1235.     if (++loop_ptr >= loop_max) {
  1236.     loop_max += 128;
  1237.     Renew(loop_stack, loop_max, struct loop);
  1238.     }
  1239.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1240.     loop_stack[loop_ptr].loop_sp = sp;
  1241. #ifdef DEBUGGING
  1242.     if (debug & 4) {
  1243.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1244.     }
  1245. #endif
  1246.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1247.     st = stack->ary_array;
  1248.     sp = arglast[0];
  1249.     if (gimme != G_ARRAY)
  1250.         st[++sp] = &str_undef;
  1251.     }
  1252.     else {
  1253.     sp = cmd_exec(cmd,gimme,sp);
  1254.     st = stack->ary_array;
  1255. /*    for (i = arglast[0] + 1; i <= sp; i++)
  1256.         st[i] = str_mortal(st[i]);  not needed, I think */
  1257.                 /* if we don't save result, free zaps it */
  1258.     }
  1259.  
  1260.  
  1261.     in_eval--;
  1262. #ifdef DEBUGGING
  1263.     if (debug & 4) {
  1264.     char *tmps = loop_stack[loop_ptr].loop_label;
  1265.     deb("(Popping label #%d %s)\n",loop_ptr,
  1266.         tmps ? tmps : "" );
  1267.     }
  1268. #endif
  1269.     loop_ptr--;
  1270.     tmps_base = oldtmps_base;
  1271.     curspat = oldspat;
  1272.     lastspat = oldlspat;
  1273.     curcmd = oldcurcmd;
  1274.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1275.     restorelist(oldsave);
  1276.  
  1277.  
  1278.     return sp;
  1279. }
  1280.  
  1281.  
  1282. /* This routine handles any switches that can be given during run */
  1283.  
  1284.  
  1285. static char *
  1286. moreswitches(s)
  1287. char *s;
  1288. {
  1289.     int numlen;
  1290.  
  1291.  
  1292.     switch (*s) {
  1293.     case '0':
  1294.     nrschar = scanoct(s, 4, &numlen);
  1295.     nrs = nsavestr("\n",1);
  1296.     *nrs = nrschar;
  1297.     if (nrschar > 0377) {
  1298.         nrslen = 0;
  1299.         nrs = "";
  1300.     }
  1301.     else if (!nrschar && numlen >= 2) {
  1302.         nrslen = 2;
  1303.         nrs = "\n\n";
  1304.         nrschar = '\n';
  1305.     }
  1306.     return s + numlen;
  1307.     case 'a':
  1308.     minus_a = TRUE;
  1309.     s++;
  1310.     return s;
  1311.     case 'c':
  1312.     minus_c = TRUE;
  1313.     s++;
  1314.     return s;
  1315.     case 'd':
  1316. #ifdef TAINT
  1317.     if (euid != uid || egid != gid)
  1318.         fatal("No -d allowed in setuid scripts");
  1319. #endif
  1320.     perldb = TRUE;
  1321.     s++;
  1322.     return s;
  1323.     case 'D':
  1324. #ifdef DEBUGGING
  1325. #ifdef TAINT
  1326.     if (euid != uid || egid != gid)
  1327.         fatal("No -D allowed in setuid scripts");
  1328. #endif
  1329.     debug = atoi(s+1) | 32768;
  1330. #else
  1331.     warn("Recompile perl with -DDEBUGGING to use -D switch\n");
  1332. #endif
  1333.     /*SUPPRESS 530*/
  1334.     for (s++; isDIGIT(*s); s++) ;
  1335.     return s;
  1336.     case 'i':
  1337.     inplace = savestr(s+1);
  1338.     /*SUPPRESS 530*/
  1339.     for (s = inplace; *s && !isSPACE(*s); s++) ;
  1340.     *s = '\0';
  1341.     break;
  1342.     case 'I':
  1343. #ifdef TAINT
  1344.     if (euid != uid || egid != gid)
  1345.         fatal("No -I allowed in setuid scripts");
  1346. #endif
  1347.     if (*++s) {
  1348.         (void)apush(stab_array(incstab),str_make(s,0));
  1349.     }
  1350.     else
  1351.         fatal("No space allowed after -I");
  1352.     break;
  1353.     case 'l':
  1354.     minus_l = TRUE;
  1355.     s++;
  1356.     if (isDIGIT(*s)) {
  1357.         ors = savestr("\n");
  1358.         orslen = 1;
  1359.         *ors = scanoct(s, 3 + (*s == '0'), &numlen);
  1360.         s += numlen;
  1361.     }
  1362.     else {
  1363.         ors = nsavestr(nrs,nrslen);
  1364.         orslen = nrslen;
  1365.     }
  1366.     return s;
  1367.     case 'n':
  1368.     minus_n = TRUE;
  1369.     s++;
  1370.     return s;
  1371.     case 'p':
  1372.     minus_p = TRUE;
  1373.     s++;
  1374.     return s;
  1375.     case 'u':
  1376.     do_undump = TRUE;
  1377.     s++;
  1378.     return s;
  1379.     case 'U':
  1380.     unsafe = TRUE;
  1381.     s++;
  1382.     return s;
  1383.     case 'v':
  1384.     fputs("\nThis is perl, version 4.0\n\n",stdout);
  1385.     fputs(rcsid,stdout);
  1386.     fputs("\nCopyright (c) 1989, 1990, 1991, Larry Wall\n",stdout);
  1387. #ifdef MSDOS
  1388.     fputs("MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n",
  1389.     stdout);
  1390. #ifdef OS2
  1391.         fputs("OS/2 port Copyright (c) 1990, Raymond Chen, Kai Uwe Rommel\n",
  1392.         stdout);
  1393. #endif
  1394. #endif
  1395.     fputs("\n\
  1396. Perl may be copied only under the terms of either the Artistic License or the\n\
  1397. GNU General Public License, which may be found in the Perl 4.0 source kit.\n",stdout);
  1398. #ifdef MSDOS
  1399.         usage(origargv[0]);
  1400. #endif
  1401.     exit(0);
  1402.     case 'w':
  1403.     dowarn = TRUE;
  1404.     s++;
  1405.     return s;
  1406.     case ' ':
  1407.     case '\n':
  1408.     case '\t':
  1409.     break;
  1410.     default:
  1411.     fatal("Switch meaningless after -x: -%s",s);
  1412.     }
  1413.     return Nullch;
  1414. }
  1415.  
  1416.  
  1417. /* compliments of Tom Christiansen */
  1418.  
  1419.  
  1420. /* unexec() can be found in the Gnu emacs distribution */
  1421.  
  1422.  
  1423. my_unexec()
  1424. {
  1425. #ifdef UNEXEC
  1426.     int    status;
  1427.     extern int etext;
  1428.     static char dumpname[BUFSIZ];
  1429.     static char perlpath[256];
  1430.  
  1431.  
  1432.     sprintf (dumpname, "%s.perldump", origfilename);
  1433.     sprintf (perlpath, "%s/perl", BIN);
  1434.  
  1435.  
  1436.     status = unexec(dumpname, perlpath, &etext, sbrk(0), 0);
  1437.     if (status)
  1438.     fprintf(stderr, "unexec of %s into %s failed!\n", perlpath, dumpname);
  1439.     exit(status);
  1440. #else
  1441. #ifdef MSDOS
  1442.     abort();    /* nothing else to do */
  1443. #else /* ! MSDOS */
  1444. #   ifndef SIGABRT
  1445. #    define SIGABRT SIGILL
  1446. #   endif
  1447. #   ifndef SIGILL
  1448. #    define SIGILL 6        /* blech */
  1449. #   endif
  1450.     kill(getpid(),SIGABRT);    /* for use with undump */
  1451. #endif /* ! MSDOS */
  1452. #endif
  1453. }
  1454.  
  1455.  
  1456.